package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class Modify implements IOperation{
    public void work(BookList bookList) {
        //只能改价格
        System.out.println("==修改图书==");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入图书名字:>");
        String name = scanner.nextLine();

        int index=-1;
        int currentUsedSize = bookList.getUsedSize();
        for (int i = 0; i < currentUsedSize; i++) {
            Book book = bookList.getPosBooks(i);
            if(book.getName().equals(name)) {//字符串的比较，用equals
                index=i;
                break;
            }
        }
        if(index==-1){
            System.out.println("没有你要修改的书！");
        }else{
            System.out.println("请输入你要修改成的相关信息:>");
            System.out.println("请输入图书名字:>");
            String mName = scanner.nextLine();
            System.out.println("请输入作者姓名:>");
            String mAuthor = scanner.nextLine();
            System.out.println("请输入图书价格:>");
            int mPrice= scanner.nextInt();
            scanner.nextLine();//接收上一步留下的回车键
            System.out.println("请输入图书类型:>");
            String mType = scanner.nextLine();

            //创建一个Book类型的对象存储这本书
            Book book = new Book(mName,mAuthor,mPrice,mType);
            //把书放进书架(这里相当于尾插)

            bookList.setPosBooks(index,book);
            System.out.println("修改成功！");
        }
    }
}
